import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../components/Login.vue'
import Home_HR from '../components/Home/Home_HR.vue'
import Home_admin from '../components/Home/Home_admin.vue'
import Home_com from '../components/Home/Home_com.vue'
import Home_emp from '../components/Home/Home_emp.vue'
import Admin_info from '../components/info/Admin_info.vue'
import Com_info from '../components/info/Com_info.vue'
import Emp_info from '../components/info/Emp_info.vue'
import HR_info from '../components/info/HR_info.vue'
import Position_HR from '../components/Position/Position_HR.vue'
import Position_admin from '../components/Position/Position_admin.vue'
import Position_com from '../components/Position/Position_com.vue'
import Position_emp from '../components/Position/Position_emp.vue'
import HR_myPos from '../components/Position/HR_myPos.vue'
import HR_Pro from '../components/Process/HR_Pro.vue'
import Emp_Pro from '../components/Process/Emp_Pro.vue'
import AllHR from '../components/AllHR.vue'
import AllCom from '../components/AllCom.vue'
import Choice from '../components/Choice.vue'
import Forget from '../components/Forget.vue'
import Register_com from '../components/Register/Register_com.vue'
import Register_emp from '../components/Register/Register_emp.vue'
import Register_HR from '../components/Register/Register_HR.vue'

const routes = [
  //重定向，访问'/'时重定向到'/login'
  { path: '/', redirect: '/login' },
  { 
    path: '/login', 
    component: Login
  },
  { 
    path: '/home_HR', 
    component: Home_HR, 
    children: [
      { path: '/HR_info', component: HR_info },
      { path: '/Position_HR', component: Position_HR },
      { path: '/HR_myPos', component: HR_myPos },
      { path: '/HR_Pro', component: HR_Pro }
    ] 
  },
  { 
    path: '/home_admin', 
    component: Home_admin, 
    children: [
      { path: '/Admin_info', component: Admin_info },
      { path: '/Position_admin', component: Position_admin },
      { path: '/AllCom', component: AllCom }
    ]
  },
  { 
    path: '/home_com', 
    component: Home_com, 
    children: [
      { path: '/Com_info', component: Com_info },
      { path: '/Position_com', component: Position_com },
      { path: '/AllHR', component: AllHR },
    ] 
  },
  { 
    path: '/home_emp', 
    component: Home_emp, 
    children: [
      { path: '/Emp_info', component: Emp_info },
      { path: '/Position_emp', component: Position_emp },
      { path: '/Emp_Pro', component: Emp_Pro }
    ] 
  },
  {
    path: '/choice',
    component: Choice
  },
  {
    path: '/forget',
    component: Forget
  },
  {
    path: '/Register_com',
    component: Register_com
  },
  {
    path: '/Register_emp',
    component: Register_emp
  },
  {
    path: '/Register_HR',
    component: Register_HR
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//挂载路由导航守卫  next() 放行 next('/login') 强制跳转
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next();
  if(to.path === '/choice') return next();
  if(to.path === '/forget') return next();
  if(to.path === '/Register_com') return next();
  if(to.path === '/Register_emp') return next();
  if(to.path === '/Register_HR') return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
